<?php

App::uses('AppController', 'Controller');

/**
 * Configuracoes Controller
 *
 * @property Configuracao $Configuracao
 * @property PaginatorComponent $Paginator
 */
class ConfiguracoesController extends ConfiguracoesAppController {

    /**
     * __construct
     *
     * @var mixed
     * @var mixed
     */
    public function __construct($request = null, $response = null) {
        parent::__construct($request, $response);
        $this->set('title', __d('configuracoes', 'Configurações'));
    }

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator', 'RequestHandler');

    /* --------------------------------------     Admin    ----------------------------------------- */

    /**
     * index method
     *
     * @return void
     */
    public function admin_index() {
        $this->admin_edit();
        $this->render('admin_add');
    }

    /**
     * admin_edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit() {
        if ($this->request->is(array('post', 'put'))) {
            foreach ($this->request->data['Configuracao'] as $key => $value) {
                $this->Configuracao->save(array('Configuracao' => array(
                        'id' => $key,
                        'valor' => $value,
                )));
                $this->Configuracao->clear();
            }
        }
        $configuracoes = $this->Configuracao->find('all');
        $this->set(compact('configuracoes'));
        $this->set('title', 'Configurações');
    }

    /**
     * sitemap method
     *
     * @return void
     */
    public function sitemap() {
        $this->layout = 'ajax';
        $this->loadModel('Conteudos.Conteudo');
        $this->loadModel('Paginas.Pagina');

        $_sitemap = $this->Conteudo->find('all', array('conditions' => array('Conteudo.status' => 1)));

        $sitemap = array();
        $i = 0;
        if (count($_sitemap) > 0) {
            foreach ($_sitemap as $key => $value) {
                $sitemap[$i]['titulo'] = $value['Conteudo']['titulo'];
                $sitemap[$i]['router'] = '/blog/' . $value['Conteudo']['router'];
                $sitemap[$i]['modified'] = $value['Conteudo']['updated'];
                $sitemap[$i]['priority'] = '1.000';
                $i++;
            }
        }
        $_sitemap = $this->Pagina->find('all', array('conditions' => array('Pagina.status' => 1)));
        if (count($_sitemap) > 0) {
            foreach ($_sitemap as $key => $value) {
                $sitemap[$i]['titulo'] = $value['Pagina']['titulo'];
                $sitemap[$i]['router'] = '/' . $value['Pagina']['router'];
                $sitemap[$i]['modified'] = $value['Pagina']['updated'];
                $sitemap[$i]['priority'] = '0.500';
                $i++;
            }
        }
        $this->set('sitemap', $sitemap);
        $this->RequestHandler->respondAs('xml');
    }

    /**
     * sitemap method
     *
     * @return void
     */
    public function rss() {
        $this->layout = 'ajax';
        $this->loadModel('Conteudos.Conteudo');
        $this->loadModel('Paginas.Pagina');

        $_sitemap = $this->Conteudo->find('all', array('conditions' => array('Conteudo.status' => 1)));

        $sitemap = array();
        $i = 0;
        if (count($_sitemap) > 0) {
            foreach ($_sitemap as $key => $value) {
                $sitemap[$i]['titulo'] = $value['Conteudo']['titulo'];
                $sitemap[$i]['router'] = '/blog/' . $value['Conteudo']['router'];
                $sitemap[$i]['descricao'] = $value['Conteudo']['conteudo'];
                $i++;
            }
        }
        $_sitemap = $this->Pagina->find('all', array('conditions' => array('Pagina.status' => 1)));
        if (count($_sitemap) > 0) {
            foreach ($_sitemap as $key => $value) {
                $sitemap[$i]['titulo'] = $value['Pagina']['titulo'];
                $sitemap[$i]['router'] = '/' . $value['Pagina']['router'];
                $sitemap[$i]['descricao'] = '';
                $i++;
            }
        }
        $this->set('sitemap', $sitemap);
        $this->RequestHandler->respondAs('xml');
    }

}
